// TodayHost.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "TodayHost.h"
#include "TodaySettings.h"

#include "resourceppc.h"
#include "list"

using namespace std;

#define MAX_LOADSTRING 100
#define IDT_TIMER1 1

// Global Variables:
HINSTANCE			g_hInst;			// current instance
HWND hostWindow;

// Forward declarations of functions included in this code module:
ATOM			MyRegisterClass(HINSTANCE, LPTSTR);
BOOL			InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

#define WM_TODAY_RELOADCONFIG (WM_USER + 250)
#define WM_MANILA_TODAY_TAB_TERMINATE (WM_USER + 24)


TODAYTABSETTINGS settings;

TodayHost* host;

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
	MSG msg;

	// Perform application initialization:
	if (!InitInstance(hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{

		TranslateMessage(&msg);
		DispatchMessage(&msg);

	}

	return (int) msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
	WNDCLASS wc;

	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = 0;
	wc.hCursor       = 0;
	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName  = 0;
	wc.lpszClassName = szWindowClass;

	return RegisterClass(&wc);
}

BOOL SortPredicate(TODAYLISTITEM i1, TODAYLISTITEM i2)
{
	return i1.dwOrder < i2.dwOrder;
}

void InitToday()
{
	list<TODAYLISTITEM>*plugins = GetAvailablePlugins();
	plugins->sort(SortPredicate);

	settings = GetMainSettings();
//	logger->Log(L"Settins.ActivePage: %s", settings.szActivePage);

	host->SetBackground(settings.szBackgroundPath);

	list<TODAYLISTITEM>::iterator i;
	for (i = plugins->begin(); i!= plugins->end(); i++)
	{
		if (i->tlit == 4 && i->fEnabled)
		{
		//	logger->Log(L"Loading plugin: %s. FileName: %s", i->szName, i->szDLLPath);
			host->LoadPlugin(*i);
		}
	}
}


BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;
    TCHAR szTitle[MAX_LOADSTRING];		// title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];	// main window class name

    g_hInst = hInstance; // Store instance handle in our global variable

    // SHInitExtraControls should be called once during your application's initialization to initialize any
    // of the device specific controls such as CAPEDIT and SIPPREF.
    SHInitExtraControls();

    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
    LoadString(hInstance, IDC_TODAYHOST, szWindowClass, MAX_LOADSTRING);

    //If it is already running, then focus on the window, and exit
    hWnd = FindWindow(szWindowClass, szTitle);	
    if (hWnd) 
    {
        // set focus to foremost child window
        // The "| 0x00000001" is used to bring any owned windows to the foreground and
        // activate them.
        SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
        return 0;
    } 

    if (!MyRegisterClass(hInstance, szWindowClass))
    {
    	return FALSE;
    }

    hostWindow = CreateWindow(szWindowClass, szTitle, WS_VISIBLE | WS_VSCROLL, 
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

    if (!hostWindow)
    {
        return FALSE;
    }

    ShowWindow(hostWindow, nCmdShow);
    UpdateWindow(hostWindow);

	host = new TodayHost(hostWindow);
	InitToday();


    return TRUE;
}





//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//


bool IsInLandscapeMode()
{
	HKEY hKey;
	RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"System\\GDI\\Rotation", NULL, KEY_QUERY_VALUE, &hKey);
	DWORD data;
	DWORD dataLen = sizeof(DWORD);
	DWORD result = RegQueryValueEx(hKey, L"Angle", 0, 0, (BYTE*)&data, &dataLen);
	RegCloseKey(hKey);
	return data == 90 || data == 270;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

  	switch (message)
	{
	case WM_TODAY_RELOADCONFIG:
		host->UnloadAllPlugins();
		InitToday();
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	case WM_SETTINGCHANGE:
		IsInLandscapeMode();
		break;
	default:
		return host->MessageHandler(hWnd, message, wParam, lParam);
	}
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_INITDIALOG:
            {
                // Create a Done button and size it.  
                SHINITDLGINFO shidi;
                shidi.dwMask = SHIDIM_FLAGS;
                shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
                shidi.hDlg = hDlg;
                SHInitDialog(&shidi);
            }
            return (INT_PTR)TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK)
            {
                EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            break;

        case WM_CLOSE:
            EndDialog(hDlg, message);
            return TRUE;

    }
    return (INT_PTR)FALSE;
}
